import java.util.Arrays;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/7/16-07-16-10:57
 * @Description：枚举学习01
 */
/*
枚举类
    构造器私有
    对象是静态的  有name（名称）、ordinal（编号 ）
    无法被继承（final修饰），也无法继承其他类，枚举类默认继承了Enum
 */
public class enum_01 {
    public static void main(String[] args) {
        //创建枚举数组
//        Day[] days = Day.values();     //获取类中的所有枚举对象
        Day[] days = {
                Day.MONDAY, Day.TUESDAY, Day.WEDNESDAY,
                Day.THURSDAY, Day.FRIDAY, Day.SATURDAY, Day.SUNDAY
        };
        //ordinal() : 返回枚举常量的序数(它在枚举声明中的位置，其中初始常量序数为零)
        for (int i = 0; i < days.length; i++) {
            System.out.println("day["+i+"].ordinal():"+days[i].ordinal());
        }
        System.out.println("-----------------------------------------------");

        //通过compareTo方法比较，实际上其内部是通过ordinal()值比较的
        System.out.println("days[0].compareTo(days[1]): " + days[0].compareTo(days[1]));
        System.out.println("days[0].compareTo(days[2]): " + days[0].compareTo(days[2]));

        //获取该枚举对象的Class对象引用，当然也可以通过getClass方法
        Class<?> cla = days[0].getDeclaringClass();
        System.out.println("class: "+ cla);

        System.out.println("-----------------------------------");

        //name() 返回此枚举常量的名称，在其枚举声明中对其进行声明  == toString()
        System.out.println("days[0].name(): " + days[0].name());
        System.out.println("days[1].name(): " + days[1].name());
        System.out.println("days[2].name(): " + days[2].name());
        System.out.println("days[3].name(): " + days[3].name());

        System.out.println("-----------------------------------------");

        System.out.println("days[0].toString(): " + days[0].toString());
        System.out.println("days[1].toString(): " + days[1].toString());
        System.out.println("days[2].toString(): " + days[2].toString());
        System.out.println("days[3].toString(): " + days[3].toString());

        System.out.println("--------------------------------------------");

        //valueOf()  根据枚举类的Class对象和枚举名称获取枚举常量，（静态方法）
        Day d = Enum.valueOf(Day.class,days[0].name());
        Day d2 = Enum.valueOf(Day.class,days[0].name());
        // Class<Day> c2 = Day.class
        //Class c3 = Class.forName("Day")
        System.out.println("d: " + d);
        System.out.println("d2: " + d2);

        //values()  获取枚举类中的所有变量，作为数组返回
        //正常使用
        Day[] days2 = Day.values();
        System.out.println("days2: " + Arrays.toString(days2));   //days2: [MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY]
        Enum e = Day.MONDAY;
        //无法调用，没有此方法
        //e.values();
        //获取class对象引用    getEnumConstans()  返回该枚举类型的所有元素，如果Class对象不是枚举类型，则返回null
        Class<?> clss = e.getDeclaringClass();
        if(clss.isEnum()){
            Day[] dsz = (Day[]) clss.getEnumConstants();
            System.out.println("dsz: " + Arrays.toString(dsz));   //dsz: [MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY]
        }

        //valueOf()  传递一个name参数
        Day day = Day.valueOf("MONDAY");
        System.out.println("day: " + day);   //day: MONDAY
    }
}
enum Day{
    MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY     //对象
}
